package com.cnxjs.shs4et.service.impl;

import com.cnxjs.shs4et.common.Result;
import com.cnxjs.shs4et.entity.Comment;
import com.cnxjs.shs4et.repository.CommentRepository;
import com.cnxjs.shs4et.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    // 按照帖子和发布时间查所有评论
    @Override
    public Result findAll(int commentTopic, boolean isDesc) {
        if (isDesc)
            return Result.success(commentRepository.findAllByCommentTopicOrderByPublishTimeDesc(commentTopic));
        else
            return Result.success(commentRepository.findAllByCommentTopicOrderByPublishTimeAsc(commentTopic));
    }
    // 按照帖子和点赞数量降序查看所有评论
    @Override
    public Result findAllByLike(int commentTopic) {
        return Result.success(commentRepository.findAllByCommentTopicOrderByRecommendCountDesc(commentTopic));
    }
    // 发表评论
    @Override
    public Result add(Comment comment) {
        return Result.success(commentRepository.save(comment));
    }
    // 伪删除评论
    @Override
    public Result delete(int commentId) {
        Comment comment = commentRepository.findById(commentId);
        comment.setIsLocked(1);
        commentRepository.save(comment);
        return Result.success("删除评论成功");
    }

    @Override
    public void addLike(int likeId) {
        Comment comment = commentRepository.findById(likeId);
        comment.setRecommendCount(comment.getRecommendCount() + 1);
        commentRepository.save(comment);
    }

}
